package com.emmy.walnut.adapter;

import android.content.Context;
import android.os.SystemClock;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;

import com.emmy.walnut.R;
import com.emmy.walnut.model.Record;
import com.emmy.walnut.model.TimingObject;
import com.emmy.walnut.util.LiveDataBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 计时条适配器
 * 主要难点在于Chronometer和RecyclerView是否会混乱
 */
public class TimingAdapter extends RecyclerView.Adapter<TimingAdapter.ViewHolder> {

    private Context mContext;
    private List<Record> mList = new ArrayList<>();

    public TimingAdapter(Context context, List<Record> list) {
        mContext = context;
        if (list!=null){
            mList = list;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_timing,
                parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.nameTextView.setText(mList.get(position).getLabelName());
        final Record record = mList.get(position);

        holder.stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.chronometer.setBase(0);
                holder.chronometer.stop();
                mList.get(position).setEndTimestamp(System.currentTimeMillis());
                LiveDataBus.get().with("endTiming", Record.class).postValue(mList.remove(position));
                notifyDataSetChanged();
            }
        });

        long startTime = mList.get(position).getStartTimestamp();

        if (startTime == 0) {
            //计时器清零
            holder.chronometer.setBase(SystemClock.elapsedRealtime());
        } else {
            holder.chronometer.setBase(SystemClock.elapsedRealtime() -
                    (System.currentTimeMillis() - record.getStartTimestamp()));
        }

        startTime = mList.get(position).getStartTimestamp();

        int hours = (int) ((System.currentTimeMillis() - startTime) / 1000 / 60 / 24);
        holder.chronometer.setFormat("0" + hours + ":%s");
        holder.chronometer.start();
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameTextView;
        Chronometer chronometer;
        Button stopButton;

        ViewHolder(View view) {
            super(view);
            nameTextView = view.findViewById(R.id.activity_item_name);
            chronometer = view.findViewById(R.id.chronometer);
            stopButton = view.findViewById(R.id.stop);
        }
    }
}
